package hrms.service;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import hrms.dao.CompanyDao;
import hrms.entity.Company;
import hrms.util.PageUtil;
import hrms.util.Result;

@Service("companyService")
public class CompanyServiceImpl implements CompanyService {

	@Resource(name="companyDao")
	private CompanyDao dao;

	public Result<List<Company>> findAll(String index) {
		Result<List<Company>> result = new Result<List<Company>>();
		int row = dao.findRow();
		int num = Integer.parseInt(index.trim());
		int realIndex = num%row;
		int show ;
		if(row>12){
			show = 12;
		} else {
			show = row/2;
		}
		List<Company> list ;
		if(realIndex == 0){
			list = dao.findAll(row, row);
			List<Company> addList = dao.findAll(realIndex, realIndex+show-1);
			list.addAll(addList);
		//页面显示7条数据,判断当前定时器循环到第几条,若之后数据大于7条,直接输出
		}else if(realIndex<=row-show){
			list = dao.findAll(realIndex, realIndex+show-1);
		//数据不够7条,需要将剩余的显示完全,再从第一条开始补全7条数据
		} else {
			list = dao.findAll(realIndex, row);
			List<Company> addList = dao.findAll(1, show-(row-realIndex+1));
			list.addAll(addList);
		}
		if(list.size()!=0){
			result.setStatus(0);
			result.setMsg("查询成功");
			result.setData(list);
		} else {
			result.setStatus(1);
			result.setMsg("未找到数据");
		}
		return result;
	}

	public Result<Map<String,String>> findDetail(String companyId) {
		int id = Integer.parseInt(companyId);
		Map<String,String> map = dao.findDetail(id);
		Result<Map<String,String>> result = new Result<Map<String,String>>();
		if(map != null){
			result.setStatus(0);
			result.setMsg("内容查找成功");
			result.setData(map);
		} else {
			result.setStatus(1);
			result.setMsg("未找到数据");
		}
		return result;
	}

	public Result<List<Company>> findByPage(String pages ,HttpServletResponse res) {
		String message = "Company";
		return new PageUtil<Company>().findByPage(pages, res, dao, message);
	}
	
	public Result<Map<String, Integer>> pageTotal(HttpServletRequest req){
		String message = "Company";
		return PageUtil.pageTotal(req, message);
	}
	
	public Result<Object> updateCompanyInfo(HttpServletRequest req) {
		Result<Object> result = new Result<Object>();
		Company com = new Company();
		com.setCompanyId(Integer.parseInt(req.getParameter("companyId")));
		com.setTitle(req.getParameter("title"));
		com.setDetail(req.getParameter("detail"));
		com.setComdate(Date.valueOf((req.getParameter("comdate"))));
		Cookie[] c = req.getCookies();
		String page = "1";
		for(Cookie cc : c){
			if(cc.getName().equals("CompanyPage")){
				page = cc.getValue();
			}
		}
		int num = dao.updateCompanyInfo(com);
		if( num!=1){
			result.setStatus(1);
			result.setMsg("更新失败");
		} else {
			result.setStatus(0);
			result.setMsg("更新成功");
			result.setData(page);
		}
		return result;
	}

	public Result<Object> addCompanyInfo(HttpServletRequest req) {
		Result<Object> result = new Result<Object>();
		Company com = new Company();
		com.setTitle(req.getParameter("title"));
		com.setDetail(req.getParameter("detail"));
		com.setComdate(Date.valueOf((req.getParameter("comdate"))));
		int num = dao.addCompanyInfo(com);
		if(num != 1){
			result.setStatus(1);
			result.setMsg("插入Company失败");
		} else {
			result.setStatus(0);
			result.setMsg("插入Company成功");					
		}
		return result;
	}

	public Result<Object> deleteCompanyInfo(String companyId ,HttpServletRequest req) {
		Result<Object> result = new Result<Object>();
		int id = Integer.parseInt(companyId);
		int num = dao.deleteCompanyInfo(id);
		Cookie[] c = req.getCookies();
		String page = "1";
		for(Cookie cc : c){
			if(cc.getName().equals("CompanyPage")){
				page = cc.getValue();
			}
		}
		int pageNum = Integer.parseInt(page);
		if(num != 1){
			result.setStatus(1);
			result.setMsg("删除出错");
		} else {
			result.setStatus(0);
			result.setMsg("删除成功");
			result.setData(pageNum);
		}
		return result;
	}

	public Result<Company> findOne(String companyId) {
		Result<Company> result = new Result<Company>();
		Company c = dao.findOne(Integer.parseInt(companyId));
		if(c!=null){
			result.setStatus(0);
			result.setMsg("获取Company成功");
			result.setData(c);
		} else {
			result.setStatus(1);
			result.setMsg("获取company失败");
		}
		return result;
	}

	public Result<List<Company>> search(String name) {
		// TODO Auto-generated method stub
		return null;
	}

}
